package com.cloudwise.monitoring_system.util;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.sql.*;
import java.util.Map;

/**
 * @author Reisen
 * @description:链接clickhouse工具类
 * @date 2021/5/31 14:10
 */
public class BaseDao {
    private static String url="jdbc:clickhouse://192.168.243.121:8123/monitor?useUnicode=true&characterEncoding=UTF-8&autoReconnect=true&serverTimezone=Asia/Shanghai";
    private static String username="root";
    private static String password="root";
    //加载驱动
    static{
        try {
            Class<?> aClass = Class.forName("ru.yandex.clickhouse.ClickHouseDriver");
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }
    }
    //获取链接
    public static Connection getConn(){
        Connection connection=null;
        try {
             connection = DriverManager.getConnection(url, username, password);
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
        return connection;
    }


    //新增方法
    public static int  insert(String sql){
        PreparedStatement ps =null;
        Connection conn = BaseDao.getConn();
        try {
             ps = conn.prepareStatement(sql);
            int i = ps.executeUpdate();
            if (i>0){
                return i;
            }
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }finally {
            BaseDao.closeResource(conn,ps,null);
        }

        return -1;
    }
    //查询方法
    public static List<Map> queryAll(String sql){
        PreparedStatement ps =null;
        Connection conn = BaseDao.getConn();
        ResultSet resultSet =null;
        try {
            ps=conn.prepareStatement(sql);
            resultSet = ps.executeQuery();
            ResultSetMetaData rsmd = resultSet.getMetaData();
            List<Map> list = new ArrayList();
            while(resultSet.next()){
                Map map = new HashMap();
                for(int i = 1;i<=rsmd.getColumnCount();i++){
                    map.put(rsmd.getColumnName(i),resultSet.getString(rsmd.getColumnName(i)));
                }
                list.add(map);
            }
            if (list!=null){
                return list;
            }

        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }finally {
            BaseDao.closeResource(conn,ps,resultSet);
        }
        return null;

    }

    //关闭资源
    public static void closeResource(Connection conn , PreparedStatement ps , ResultSet rs){

        if(rs != null){
            try {
                rs.close();
            } catch (SQLException e) {
            }
        }

        if(ps != null){
            try {
                ps.close();
            } catch (SQLException e) {
            }
        }

        if(conn != null){
            try {
                conn.close();
            } catch (SQLException e) {
            }
        }

    }
}
